# --*-- coding:utf-8 --*--

import requests
import random
import time
import json
import re

import warnings
warnings.filterwarnings("ignore")
import jieba


def select_ten(list1):
		keyword = []
		value = []
		ratio = []
		b = {}              
		for d in set(list1):    
		    b[list1.count(d)] = d
		sum_value = 0
		for e in reversed(sorted(b.keys())[-100:]):
			keyword.append(b[e])
			value.append(e)
			sum_value = sum_value + e

		# 计算占比

		for values in value :
			ratio.append("%.2f%%" % ((values/sum_value) * 100))

		return {'keyword':keyword,'value':value,'ratio':ratio}


def keyword_participle(text):

	text_list = []
	seg_list = jieba.cut(text, cut_all=False)
	for i in seg_list:
		text_list.append(i)
	return text_list

 
def SkuComent(itemid,maxPage,cookies):

	Sku = []
	Comment = []
	Analysis_Comment = []
	dele_str = "'，,。、【 】 “”：；（）《》‘’{}？！⑦()、%^>℃：.”“^-——=&#@￥!～" # 定义需要删除的字符串	

	page = 1
	while True:
		url = 'https://rate.taobao.com/feedRateList.htm?auctionNumId={}&userNumId=1048157570&currentPageNum={}&pageSize=20'.format(itemid,str(page))
		
		headers = {
				'Host': 'rate.taobao.com',
				'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
				'Accept': '*/*',
				'Connection': 'keep-alive',
				'Accept-Encoding': 'gzip, deflate'

		}

		resp = requests.get(url,verify=False,cookies = cookies,headers = headers)
		data  = json.loads(resp.text.strip().strip('()'))
		try:
			data  = json.loads(resp.text.strip().strip('()'))
			if data['comments'] != None:
				for i in data['comments']:
					Sku.append((i['auction']['sku']))
					if i['content'] != "此用户没有填写评价。" and i['content'] != "评价方未及时做出评价,系统默认好评!":  # 去掉默认评价
						Comment.append((i['content']))
					for key in keyword_participle(i['content']):
						if key not in dele_str and len(key) >= 2:
							Analysis_Comment.append(key)

				if page >= maxPage:  #完成所需数据返回结果
					analysis = {}
					for i in Sku:
						if Sku.count(i) > 0:
							analysis[i] = Sku.count(i)  #数据处理
					return {'sku':analysis,'comment':Comment,'Analysis_Comment':select_ten(Analysis_Comment)}

				else:  # 翻页
					page += 1
					# 时间间隔
					time.sleep(random.uniform(4,5))

			else:
				#后面没数据了
				analysis = {}
				for i in Sku:
					if Sku.count(i) > 0:
						analysis[i] = Sku.count(i)  #数据处理
				return {'sku':analysis,'comment':Comment,'Analysis_Comment':select_ten(Analysis_Comment)}

		except: 
			# 失败翻页，时间间隔
			page += 1
			time.sleep(random.uniform(1,2))
			if page >= maxPage:
				analysis = {}
				for i in Sku:
					if Sku.count(i) > 0:
						analysis[i] = Sku.count(i)  #数据处理
				return {'sku':analysis,'comment':Comment,'Analysis_Comment':select_ten(Analysis_Comment)}




		# try:
		# 	data = json.loads(re.match(".*?({.*}).*", resp.text, re.S).group(1))
		# 	rateList = data['rateDetail']['rateList']
		# 	if rateList != []:
		# 		for rate in rateList:
		# 			for key in keyword_participle(rate['rateContent']):
		# 				if key not in dele_str and len(key) >= 2:
		# 					Analysis_Comment.append(key)
		# 			Sku.append(rate['auctionSku'])
		# 			Comment.append(rate['rateContent'])

		# 		# 翻页
		# 		if i >= maxPage:
		# 			analysis = {}
		# 			for i in Sku:
		# 				if Sku.count(i) > 0:
		# 					analysis[i] = Sku.count(i)  #数据处理
		# 			return {'sku':analysis,'comment':Comment,'Analysis_Comment':select_ten(Analysis_Comment)}

		# 		else:
		# 			i = i+1

		# 			# 时间间隔
		# 			time.sleep(random.uniform(2,3))
		# 	else:
		# 		# 后面没数据了

		# 		analysis = {}
		# 		for i in Sku:
		# 			if Sku.count(i) > 0:
		# 				analysis[i] = Sku.count(i)  #数据处理
		# 		return {'sku':analysis,'comment':Comment,'Analysis_Comment':select_ten(Analysis_Comment)}

		



if __name__ == '__main__':
	
	print (SkuComent('44407841142',100))
	


# https://rate.tmall.com/list_detail_rate.htm?itemId=44407841142&order=3&sellerId=1914459560&currentPage=1